package io.awen.config;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

/**
 * @ClassName: MqttConfig
 * @Description: TODO
 * @Author: liujianfu
 * @Date: 2021/08/16 14:43:39
 * @Version: V1.0
 **/
@Component
@ConfigurationProperties("spring.mqtt")
@Data
public class MqttConfig {

    @Autowired
    private MqttPushClient mqttPushClient;

    /**
     * 用户名
     */
    private String username;
    /**
     * 密码
     */
    private String password;
    /**
     * 连接地址
     */
    private String hostUrl;
    /**
     * 客户Id
     */
    private String clientId;
    /**
     * 液位topic
     */
    private String defaultTopic;
    /**
     * 车辆topic
     */
    private String defaultTopic1;
    /**
     * 超时时间
     */
    private int timeout;
    /**
     * 保持连接数
     */
    private int keepalive;

//    public void setMqttPushClient(MqttPushClient mqttPushClient) {
//        this.mqttPushClient = mqttPushClient;
//    }
//
//    public String getUsername() {
//        return username;
//    }
//
//    public void setUsername(String username) {
//        this.username = username;
//    }
//
//    public String getPassword() {
//        return password;
//    }
//
//    public void setPassword(String password) {
//        this.password = password;
//    }
//
//    public String getHostUrl() {
//        return hostUrl;
//    }
//
//    public void setHostUrl(String hostUrl) {
//        this.hostUrl = hostUrl;
//    }
//
//    public String getClientId() {
//        return clientId;
//    }
//
//    public void setClientId(String clientId) {
//        this.clientId = clientId;
//    }
//
//    public String getDefaultTopic() {
//        return defaultTopic;
//    }
//
//    public void setDefaultTopic(String defaultTopic) {
//        this.defaultTopic = defaultTopic;
//    }
//
//
//    public int getTimeout() {
//        return timeout;
//    }
//
//    public void setTimeout(int timeout) {
//        this.timeout = timeout;
//    }
//
//    public int getKeepalive() {
//        return keepalive;
//    }
//
//    public void setKeepalive(int keepalive) {
//        this.keepalive = keepalive;
//    }

    @Bean
    public MqttPushClient getMqttPushClient() {

        mqttPushClient.connect(hostUrl, clientId, username, password, timeout, keepalive,new String[]{ defaultTopic, defaultTopic1 });
        // 以/#结尾表示订阅所有以test开头的主题

        mqttPushClient.subscribe(new String[]{ defaultTopic, defaultTopic1 }, 0);

        return mqttPushClient;
    }
}
